#include "FinishLevelScreen.h"
#include "ResourceManager.h"
#include "MainGameScreen.h"
#include "MenuScreen.h"

FinishLevelScreen::FinishLevelScreen(Mario* mario,int level) : IGameState()
{
	backgroud = new CSprite(ResourceManager::GetIns()->GetBackGround(Black),1,1,1,0,0);
	this->mario = mario;
	switch(mario->GetMarioState())
	{
	case EMarioState::Esmall:
		marioLife = new CSprite(ResourceManager::GetIns()->GetTexture(EResource::Mario_Small_ID),6,1,6,0,1);
		break;
	case EMarioState::Ebig:
		marioLife = new CSprite(ResourceManager::GetIns()->GetTexture(EResource::Mario_Big_ID),5,1,5,0,1);
		break;
	case EMarioState::Egun:
	case EMarioState::Esuperstar:
		marioLife = new CSprite(ResourceManager::GetIns()->GetTexture(EResource::Mario_Gun_ID),5,1,5,0,1);
		break;
	}
	marioLife->m_TimeAni = 0.1f;
	coinCount = new CSprite(ResourceManager::GetIns()->GetTexture(EResource::Coin_ID),7,1,7,0,6);
	marioPos = D3DXVECTOR2(350,200);
	coinPos = D3DXVECTOR2(350,300);
	this->level = level;
	m_text = new CText();
	mario->SetPosition(D3DXVECTOR2(300,300));
	time = MainGameScreen::time;
	score = MainGameScreen::score;
	life = MainGameScreen::life;
	coin = MainGameScreen::coin;
	timeCountCoin = 0;
	timeCountTimes = 0;
	timeWaitToNextLv = 0;

}

FinishLevelScreen::~FinishLevelScreen()
{
	IGameState::~IGameState();
}

void FinishLevelScreen::Create()
{

}

void FinishLevelScreen::Release()
{

}

void FinishLevelScreen::HandleInput(CInput* input,float dt)
{

}

void FinishLevelScreen::Update(float dt)
{
	marioLife->Update(dt);
	coinCount->Update(dt);
	if(coin > 0)
	{
		timeCountCoin+=dt;
		if(timeCountCoin > 0.1f)
		{
			coin--;
			score += 100;
			timeCountCoin = 0;
		}
	}
	else
	{
		if(time > 0)
		{
			timeCountTimes+=dt;
			if(timeCountTimes > 0.1f)
			{
				time-=5;
				score += 50;
				timeCountTimes = 0;
			}
		}
		else
		{
			time = 0;
		}
	}
	if(coin <= 0 && time <=0 && life>0 && level<3)
	{
		timeWaitToNextLv+=dt;
		if(timeWaitToNextLv >5)
		{
			mario = new Mario(D3DXVECTOR2(100,200),mario->GetMarioState(),mario->GetPreviousState());
			mario->SetScore(score);
			mario->SetLife(life);

			level+=1;
			ExitSceen();
			StateManager::GetInstance()->AddScreen(new MainGameScreen(mario,level));
		}
	}
	else
	if((coin <= 0 && time <=0 && life==0)||level==3)
	{
		timeWaitToNextLv+=dt;
		if(timeWaitToNextLv >5)
		{			
			ExitSceen();
			StateManager::GetInstance()->AddScreen(new MenuScreen());
		}
	}
}

void FinishLevelScreen::Draw()
{
	D3DXMATRIX matrix;
	D3DXMatrixIdentity(&matrix);
	GL_graphic->m_pSpriteHandler->SetTransform(&matrix);	

	marioLife->Draw(marioPos,SpriteEffect::Vertical,0,1);
	coinCount->Draw(coinPos,SpriteEffect::None,0,1);
	
	char* _text = new char;
	LPCSTR _p;
	RECT m_textRect;

	//World
	SetRect(&m_textRect,350,100,800,600);
	m_text->Draw("WORLD:   ",m_textRect,DT_LEFT,D3DCOLOR_XRGB(255,255,255));
	SetRect(&m_textRect,440,100,800,600);
	itoa(level,_text,10);
	_p = _text;
	m_text->Draw(_p,m_textRect,DT_LEFT,D3DCOLOR_XRGB(255,255,255));

	//Life
	SetRect(&m_textRect,marioPos.x+ marioLife->GetBound()._width+50,marioPos.y ,800,800);
	m_text->Draw(" X ",m_textRect,DT_LEFT,D3DCOLOR_XRGB(255,255,255));
	SetRect(&m_textRect,marioPos.x+ marioLife->GetBound()._width+100,marioPos.y,800,800);
	itoa(life,_text,10);
	_p = _text;
	m_text->Draw(_p,m_textRect,DT_LEFT,D3DCOLOR_XRGB(255,255,255));

	//Coin
	SetRect(&m_textRect,coinPos.x+ coinCount->GetBound()._width+50,coinPos.y,800,800);
	m_text->Draw(" X ",m_textRect,DT_LEFT,D3DCOLOR_XRGB(255,255,255));
	SetRect(&m_textRect,coinPos.x+ coinCount->GetBound()._width+100,coinPos.y,800,800);
	itoa(coin,_text,10);
	_p = _text;
	m_text->Draw(_p,m_textRect,DT_LEFT,D3DCOLOR_XRGB(255,255,255));

	//Time
	SetRect(&m_textRect,marioPos.x,coinPos.y + coinCount->GetBound()._height+20,800,800);
	m_text->Draw("Times:     ",m_textRect,DT_LEFT,D3DCOLOR_XRGB(255,255,255));
	SetRect(&m_textRect,marioPos.x+100,coinPos.y + coinCount->GetBound()._height+20,800,800);
	itoa(time,_text,10);
	_p = _text;
	m_text->Draw(_p,m_textRect,DT_LEFT,D3DCOLOR_XRGB(255,255,255));

	//Score
	SetRect(&m_textRect,300,450,400,800);
	m_text->Draw("Scores : ",m_textRect,DT_LEFT,D3DCOLOR_XRGB(255,255,255));
	SetRect(&m_textRect,380,450,800,800);
	itoa(score,_text,10);
	_p = _text;
	m_text->Draw(_p,m_textRect,DT_LEFT,D3DCOLOR_XRGB(255,255,255));

	
}